//功能清单
//1.新增初始化
//1.1(完成)[单据头.锁定日期] =select  getdate() from fg_orglist where ocode='000'
//1.2(完成)[单据头.锁定人] =select ccode from hr_epm_main  where cno= [单据头.录入人]
//1.3(完成)[单据头.锁定事件] =select  '交回手续' from fg_orglist where ocode='000'
//1.4(完成)[单据头.领用人] = select ccode from hr_epm_main where cno= [单据头.录入人]
//1.5(完成)[单据头.用途类别] =select top 1 ytlb   from fzyp_opteror where opteror = [单据头.录入人] and ocode = [单据头.组织] order by case when ytlb='10000' then '10007' else ytlb end
//2.删除单据检测事件
//2.1(完成) p_form0000000862_m.is_hz<>'1',该单据已核准过，不得删除！,0
//3.核准更新事件
//3.1(完成)  exec fzyp_jhd_hz(string|[单据头.单据编码])
//4.表头事件
//4.1.1(无需实现)用途类别更新事件：[单据头.用途类别名称] =select description from fzyp_ytlb where ID= [单据头.用途类别]
//4.1.2(完成)用途类别更新事件：[单据头.领用对象] =if( [单据头.用途类别]='10000','班组领用', [单据头.领用对象])
//4.2.1(完成)领用对象更新事件：[单据头.标题辅助] =select '非个人领用' from fg_orglist where ocode='000' and  [单据头.领用对象] ='非个人领用'
//4.3.1(完成)领用人更新事件：[单据头.领用人姓名] =select cname  from hr_epm_main where ccode= [单据头.领用人]
//4.4.1(完成)主要物资更新事件：[单据头.单据名称] = [单据头.标题辅助]+'交回'+ [单据头.主要物资（用于标题）]+'等总数量为'+ [单据头.锁定m表关键字]+'的物资'
//                                          {btfz}+'交回'+{zywz}+'等总数量为'+{suoding}+'的物资'
//4.5.1(完成)仓库更新事件：[单据头.仓库类型] =select cklx  from fzypck where bill_no = [单据头.仓库]
//4.5.2(完成)仓库更新事件：[单据头.提示] =select ck.title +'已于'+dbo.fun_date_to_CNname(ck.sdrq )+'因'+ck.sdsj +'被'+ck.sdr +'锁定，无法办理业务。'  from fzypck ck   where bill_no =  [单据头.仓库]
//4.5.3(完成)仓库更新事件：[单据头.所选仓库锁定] =select isnull(sd & 0)  from fzypck where bill_no =   [单据头.仓库]
//4.5.4(完成)仓库更新事件：[单据头.仓库名称]=select ck.title  from fzypck ck   where bill_no =  [单据头.仓库]
//4.5.5(完成)仓库更新事件：select a.itemno  itemno,a.yp_no  yp_no,a.yp_name yp_name  ,a.gg gg,a.cpxh cpxh,a.lyr_name lyr_name,  a.msunit dw ,ISNULL(a.qty_ff & 0)-ISNULL(qty_ff_bz & 0)  lysl,case when a.zclx in('1'  &  '2') then ISNULL(a.qty_ff & 0)  else 0 end jhsl ,a.prc0 prc0,a.prc1 prc1 ,a.zclx zclx ,a.bill_no ck, a.d_code ckd_code from fzyp_qdmx  a where bill_no =  [单据头.仓库] and isnull(qty_ff & 0) >0 and [单据头.领用对象] <>'班组领用' and (([单据头.领用对象] ='个人领用' and empid1 =[单据头.领用人]) or ([单据头.领用对象] ='非个人领用' and ISNULL(a.syzt & '')<>'个人领用'  )) order by zclx,yp_no
//  覆盖 +  用户选择
//4.6.1施工班组更新事件：select d.itemno  itemno,d.yp_no  yp_no,d.yp_name yp_name  ,d.gg gg,d.cpxh cpxh,  d.dw  dw ,d.ffsl lysl0,d.jhsl jhsl0,d.dssl dssl0,ISNULL(d.ffsl  & 0)-ISNULL(d.jhsl  & 0) -ISNULL(d.dssl  & 0) lysl ,case when d.zclx in('1'  &  '2') then 1  else 0.0 end jhsl,d.prc0 prc0,d.prc1 prc1 ,d.zclx zclx ,m.ck ck, d.ckd_code ckd_code ,d.code lyd_d_code  from p_form0000000859_m m join p_form0000000859_d1 d on m.code =d.m_code where m.ck =[单据头.仓库] and m.sgbz  =  [单据头.施工班组]  and ISNULL(d.ffsl  & 0)-ISNULL(d.jhsl  & 0)-ISNULL(d.dssl  & 0) >0
//4.7.1 追加，填写到单据体：select a.itemno  itemno,a.yp_no  yp_no,a.yp_name yp_name  ,a.gg gg,a.cpxh cpxh,a.lyr_name lyr_name,  a.msunit dw ,ISNULL(a.qty_ff & 0)  lysl,case when a.zclx in('1'  &  '2') then ISNULL(a.qty_ff & 0)  else 0 end jhsl ,a.prc0 prc0,a.prc1 prc1 ,a.zclx zclx ,a.bill_no ck, a.d_code ckd_code from fzyp_qdmx  a where bill_no =  [单据头.仓库] and isnull(qty_ff & 0) >0 and [单据头.领用对象] <>'班组领用' and (([单据头.领用对象] ='个人领用' and empid1 =[单据头.领用人]) or ([单据头.领用对象] ='非个人领用' and ISNULL(a.syzt & '')<>'个人领用'  )) order by zclx,yp_no
//4.8.1仓库名称更新事件：[单据头.单据名称] =select case [单据头.领用对象] when '个人领用' then [单据头.仓库名称]+ [单据头.领用人姓名]+'第'+[单据头.个人交回期次]+'次交回' else [单据头.仓库名称]+ '非个人领用第'+[单据头.非个人交回期次]+'次交回' end from fg_orglist where ocode='000'
//4.9.1领用人姓名更新事件：[单据头.标题辅助] = [单据头.领用人姓名]
//4.10.1施工班组名称更新事件：[单据头.标题辅助] = [单据头.施工班组名称]
//4.11.1(完成)锁定m表关键字更新事件：[单据头.单据名称] = [单据头.标题辅助]+'交回'+ [单据头.主要物资（用于标题）]+'等总数量为'+ [单据头.锁定m表关键字]+'的物资'
//4.12.1(完成)表体辅助更新事件：[单据头.单据名称] = [单据头.标题辅助]+'交回'+ [单据头.主要物资（用于标题）]+'等总数量为'+ [单据头.锁定m表关键字]+'的物资'
//5.表体事件
//5.1.1资源编码更新事件：[单据头.主要物资（用于标题）] =select top 1 case when [单据头.主要物资（用于标题）] is null or  [单据头.主要物资（用于标题）] = 'not set data' then  itemname else [单据头.主要物资（用于标题）] end  from itemdata where itemno =  [单据体1.资源编码]
//5.1.2资源编码更新事件：[单据头.交回清单]=select  itemname  from itemdata where itemno =  [单据体1.资源编码]
//5.2.1本次拟交更新事件：[单据头.锁定m表关键字]  =[单据体1.本次拟交]
//5.3.1单价原值更新事件：[单据体1.成新率(%)] = [单据体1.单价现值]  /[单据体1.单价原值]*100
//5.4.1单价现值更新事件：[单据体1.成新率(%)] = [单据体1.单价现值]  /[单据体1.单价原值]*100
//5.5.1原仓库bill_no更新事件：[单据体1.未核准交回或丢损单据数] =select (select COUNT(jhsl) from p_form0000000871_m m join   p_form0000000871_d1 d on m.code=d.m_code where isnull(m.ischeck & '0')='0' and d.ck= [单据体1.原仓库bill_no] and d.ckd_code = [单据体1.仓库d_code])+(select COUNT(jhsl) from p_form0000000862_m m join   p_form0000000862_d1 d on m.code=d.m_code where isnull(m.ischeck & '0')='0' and d.ck= [单据体1.原仓库bill_no] and d.ckd_code = [单据体1.仓库d_code]) from fg_orglist where ocode ='000'
//5.6.1仓库d_code更新事件：[单据体1.未核准交回或丢损单据数] =select (select COUNT(jhsl) from p_form0000000871_m m join   p_form0000000871_d1 d on m.code=d.m_code where isnull(m.ischeck & '0')='0' and d.ck= [单据体1.原仓库bill_no] and d.ckd_code = [单据体1.仓库d_code])+(select COUNT(jhsl) from p_form0000000862_m m join   p_form0000000862_d1 d on m.code=d.m_code where isnull(m.ischeck & '0')='0' and d.ck= [单据体1.原仓库bill_no] and d.ckd_code = [单据体1.仓库d_code]) from fg_orglist where ocode ='000'

function allReadyEdit() {
    NG._getForm().on('dataready', function () {
        if (otype != 'view') {
            if (otype == 'add') {
                NG.setValue('sdrq', NG.getValue('bill_dt'));
                var v_cgr = NG.execServer('get_hrid_by_fg3userid', {'phid': $appinfo.userID}, 'hrid');
                NG.setValue('empid3', v_cgr);
                NG.setValue('sdsj', '交回手续');
                NG.setValue('empid', v_cgr);
                var v_ytlb = NG.callServer('init_ytlb', {
                    'opteror': NG.getValue('fillpsn'),
                    'ocode': NG.getValue('ocode')
                }, 'ytlb');
                NG.setValue('ytlb', v_ytlb);
            }
            NG.getItem('ytlb').on('helpselected', function (record) {
                if (record.code == '10000') {
                    NG.setValue('lydx', 'bzly');
                }
            });
            NG.getItem('lydx').on('change', function (p1, p2, p3) {
                if (p2 == 'fgrly') {
                    NG.setValue('btfz', '非个人领用');
                }
            });

            NG.getItem('empid').on('helpselected', function () {
                NG.setValue('lyr_name', NG.getItem('empid').rawValue)
            });

            NG.bindMasterExp('title', "{btfz}+'交回'+{zywz}+'等总数量为'+{suoding}+'的物资'");

            NG.getItem('ck').on('helpselected', function (record) {
                var v_cklx = NG.callServer('获取仓库类型', {'bill_no': record.data.bill_no}, 'cklx');
                NG.setValue('cklx', v_cklx);
                var v_ckzt = NG.callServer('获取仓库锁定状态', {'bill_no': record.data.bill_no});
                if (v_ckzt.record.length > 0) {
                    NG.setValue('ck_sd', v_ckzt.record[0].sd);
                    NG.setValue('ck_name', v_ckzt.record[0].title);
                    if (v_ckzt.record[0].sd == '1') {
                        Ext.MessageBox.alert('提示', v_ckzt.record[0].title + '已于' + v_ckzt.record[0].sdrq || '' + '因' + v_ckzt.record[0].sdsj || '' + '被' + v_ckzt.record[0].sdr + '锁定，无法办理业务。', function () {
                            window.close();
                        });
                    }
                    var v_lydx = NG.getValue('lydx');
                    var v_empid = NG.getValue('empid');
                    if (v_lydx && v_empid) {
                        NG.addDetailByUser('获取仓库资源', {
                            'bill_no': record.data.bill_no,
                            'lydx': v_lydx,
                            'empid': v_empid
                        }, '选择资源', 600, 400, null, true);
                    }
                    else {
                        NG.alert('请先选择领用对象和领用人再选择仓库');
                    }
                }

            });

        }
    });
}

function beforeDeleteList() {
    var v_ischeck = curGrid.getSelectionModel().getSelection()[0].data.ischeck;
    if (v_ischeck == '1') {
        NG.alert('单据已核准，不允许删除');
        return false;
    } else {
        return true;
    }
}

function getSaveDataList(type) {
    if (type == 'Verify') {
        return {
            funcname: 'fzyp_jhd_hz',
            paramstr: [{'bill_no': NG._getGrid().getSelectionModel().getSelection()[0].data.bill_no}]
        };
    }
}
